/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.enterprise.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.entity.Content;
import org.springblade.modules.enterprise.mapper.ContentMapper;
import org.springblade.modules.enterprise.service.IContentService;
import org.springblade.modules.enterprise.vo.ContentVO;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章 服务实现类
 *
 * @author BladeX
 * @since 2021-08-09
 */
@Service
public class ContentServiceImpl extends BaseServiceImpl<ContentMapper, Content> implements IContentService {



	@Override
	public IPage<ContentVO> selectContentPage(IPage<ContentVO> page, ContentVO content) {
		return page.setRecords(baseMapper.selectContentPage(page, content));
	}

	@Override
	public IPage<Content> selectSubContentPage(IPage<Content> page, Integer[] area, Integer[] prod, Integer[] industry) {
		final List<String> areaStr = Arrays.stream(area).map(item -> "," + item + ",").collect(Collectors.toList());
		final List<String> prodStr = Arrays.stream(prod).map(item -> "," + item + ",").collect(Collectors.toList());
		final List<String> industryStr = Arrays.stream(industry).map(item -> "," + item + ",").collect(Collectors.toList());
		return baseMapper.selectSubContentPage(page, areaStr, prodStr, industryStr);
	}

	@Override
	public IPage<Content> selectFlList(IPage<Content> page, String contentTitle, String area, String prod, String industry, String contentType) {
		// 处理查询条件
		area = appendDot(area);
		prod = appendDot(prod);
		industry = appendDot(industry);
		return baseMapper.selectFlList(page, contentTitle, area, prod, industry, contentType);
	}

	private String appendDot(String contentStr) {
		//如果前后没有","则追加
		if (Func.isNotBlank(contentStr)) {
			String dot = ",";
			if (!contentStr.startsWith(dot)) {
				contentStr = dot + contentStr;
			}
			if (!contentStr.endsWith(dot)) {
				contentStr = contentStr + dot;
			}
		}
		return contentStr;
	}

}
